package org.stwy.report;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WritableWorkbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rap.common.entity.BaseEntity;
import com.rap.common.report.ReportModel;
import com.rap.common.table.TableModel;
import com.rap.common.table.TableUtils;
import com.rap.common.utils.FileUtils;
import com.rap.frame.web.ActionResultModel;
import com.rap.frame.web.BaseController;

@Controller
@RequestMapping(value = "/stwy/report")
public class StwyReportController {

	@Autowired
	private StwyReportService stwyReportService;
	
	@RequestMapping(value = "/report1")
	public String report1(Model model) {
		model.addAttribute("reportName", "report1");
		return "stwy/report/report";
	}
	@RequestMapping(value = "/report2")
	public String report2(Model model) {
		model.addAttribute("reportName", "report2");
		return "stwy/report/report";
	}
	@RequestMapping(value = "/report3")
	public String report3(Model model) {
		model.addAttribute("reportName", "report3");
		return "stwy/report/report";
	}
	@RequestMapping(value = "/report4")
	public String report4(Model model) {
		model.addAttribute("reportName", "report4");
		return "stwy/report/report";
	}
	@RequestMapping(value = "/report5")
	public String report5(Model model) {
		model.addAttribute("reportName", "report5");
		return "stwy/report/report";
	}
	@RequestMapping(value = "/report6")
	public String report6(Model model) {
		model.addAttribute("reportName", "report6");
		return "stwy/report/report";
	}
	@RequestMapping(value = "/report7")
	public String report7(Model model) {
		model.addAttribute("reportName", "report7");
		return "stwy/report/report";
	}
	@RequestMapping(value = "/report8")
	public String report8(Model model) {
		return "stwy/report/report8";
	}
	@RequestMapping(value = "/report9")
	public String report9(Model model) {
		model.addAttribute("reportName", "report9");
		return "stwy/report/report";
	}
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/loadReport")
	@ResponseBody
	protected ActionResultModel loadReport(ServletRequest request,String reportName) {
		ActionResultModel arm = new ActionResultModel();
		try {
			ReportModel reportModel = stwyReportService.getReport(request.getParameterMap(),reportName);
			TableModel tableModel = stwyReportService.reportToTable(reportModel);
			arm.setRecords(tableModel);
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/loadReport8")
	@ResponseBody
	protected ActionResultModel loadReport8(ServletRequest request) {
		ActionResultModel arm = new ActionResultModel();
		try {
			Map map = stwyReportService.getReport8(request.getParameterMap());
			arm.setRecords(map);
			arm.setSuccess(true);
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	
	/**
	 * 导出Excel
	 * @author wzw 2014年11月6日
	 * @param request
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/exportReport")
	public void exportReport(HttpServletRequest request, HttpServletResponse response,String reportName) {
		try{
			File tempFile =FileUtils.createTempFile();
			ReportModel reportModel = stwyReportService.getReport(request.getParameterMap(),reportName);
			TableModel tableModel = this.stwyReportService.reportToTable(reportModel);
//			WritableWorkbook workbook = TableUtils.getExcelFromTableList(tableList,tempFile);
			WritableWorkbook workbook = TableUtils.getExcelFromTableModel(tableModel, tempFile);
			workbook.write();
			workbook.close();
			FileUtils.writeFileToResponse(tempFile, response,"报表.xls",request);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
